package com.ctg.behavior.calc.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.utils.CtDateUtils;

public class CtCalcDaysUtils {

	public static List<Integer> ltvNS = new ArrayList<>();
	static {
		ltvNS.add(0);
		ltvNS.add(1);
		ltvNS.add(2);
		ltvNS.add(3);
		ltvNS.add(4);
		ltvNS.add(5);
		ltvNS.add(6);
		ltvNS.add(7);
		ltvNS.add(15);
		ltvNS.add(30);
		ltvNS.add(60);
		ltvNS.add(90);
	}

	public static String getFieldOfByTime(String unit) {
		if (CtCalcConsts.UNIT_TYPE_MONTH.equals(unit)) {
			return "t_month";
		} else if (CtCalcConsts.UNIT_TYPE_WEEK.equals(unit)) {
			return "t_week";
		} else if (CtCalcConsts.UNIT_TYPE_DAY.equals(unit)) {
			return "t_day";
		} else if (CtCalcConsts.UNIT_TYPE_HOUR.equals(unit)) {
			return "t_hour";
		} else if (CtCalcConsts.UNIT_TYPE_MINUTE.equals(unit)) {
			return "t_minute";
		}
		return "";
	}

	public static Boolean isOnlyToday(CtCalcDate calcDate) {
		Date todayStart = CtDateUtils.formatDayStartTime(new Date());
		Date todayEnd   = CtDateUtils.formatDayEndTime(new Date());
		if (todayStart.compareTo(calcDate.getFromDate()) <= 0 && todayEnd.compareTo(calcDate.getToDate()) >= 0) {
			return true;
		}
		return false;
	}

	public static Boolean isContainToday(CtCalcDate calcDate) {
		String today = CtDateUtils.dateToString(new Date(), CtDateUtils.YYYY_MM_DD);
		String toDate = CtDateUtils.dateToString(calcDate.getToDate(), CtDateUtils.YYYY_MM_DD);
		String fromDate = CtDateUtils.dateToString(calcDate.getFromDate(), CtDateUtils.YYYY_MM_DD);
		if (toDate.compareTo(today) >= 0 && fromDate.compareTo(today) <= 0) {
			return true;
		}
		return false;
	}

	public static List<CtCalcDate> listCalcMinutes(String fromDate, String toDate) {
		List<CtCalcDate> hours = new ArrayList<CtCalcDate>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		dtToDate = CtDateUtils.formatDayEndTime(dtToDate);
		while (dtToDate.compareTo(dtFromDate) >= 0) {

			Date theEndOfHour = CtDateUtils.formatMinuteEndTime(dtFromDate);
			if (theEndOfHour.compareTo(dtToDate) > 0) {
				theEndOfHour = dtToDate;
			}

			CtCalcDate oneDate = new CtCalcDate();
			//oneDate.setUnit(CtCalcConsts.UNIT_TYPE_MINUTE);
			oneDate.setFromDate(new Date(dtFromDate.getTime()));
			oneDate.setToDate(new Date(theEndOfHour.getTime()));
			hours.add(oneDate);

			dtFromDate = CtDateUtils.addMinute(dtFromDate, 1);
		}
		return hours;
	}

	public static List<CtCalcDate> listCalcHours(String fromDate, String toDate) {
		List<CtCalcDate> hours = new ArrayList<CtCalcDate>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		dtToDate = CtDateUtils.formatDayEndTime(dtToDate);
		while (dtToDate.compareTo(dtFromDate) >= 0) {

			Date theEndOfHour = CtDateUtils.formatHourEndTime(dtFromDate);
			if (theEndOfHour.compareTo(dtToDate) > 0) {
				theEndOfHour = dtToDate;
			}

			CtCalcDate oneDate = new CtCalcDate();
			//oneDate.setUnit(CtCalcConsts.UNIT_TYPE_HOUR);
			oneDate.setFromDate(new Date(dtFromDate.getTime()));
			oneDate.setToDate(new Date(theEndOfHour.getTime()));
			hours.add(oneDate);

			dtFromDate = CtDateUtils.addHour(dtFromDate, 1);
		}
		return hours;
	}

	public static List<String> listCalcDayList(String fromDate, String toDate) {
		List<String> days = new ArrayList<String>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		while (dtToDate.compareTo(dtFromDate) >= 0) {
			String theDay = CtDateUtils.dateToString(dtFromDate, CtDateUtils.YYYY_MM_DD);
			days.add(theDay);
			dtFromDate = CtDateUtils.addDate(dtFromDate, 1);
		}
		return days;
	}

	public static List<CtCalcDate> listCalcDays(String fromDate, String toDate) {
		List<CtCalcDate> days = new ArrayList<CtCalcDate>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		dtToDate = CtDateUtils.formatDayEndTime(dtToDate);
		while (dtToDate.compareTo(dtFromDate) >= 0) {

			Date theEndOfDay = CtDateUtils.formatDayEndTime(dtFromDate);
			if (theEndOfDay.compareTo(dtToDate) > 0) {
				theEndOfDay = dtToDate;
			}

			CtCalcDate oneDate = new CtCalcDate();
			//oneDate.setUnit(CtCalcConsts.UNIT_TYPE_DAY);
			oneDate.setFromDate(new Date(dtFromDate.getTime()));
			oneDate.setToDate(new Date(theEndOfDay.getTime()));
			days.add(oneDate);

			dtFromDate = CtDateUtils.addDate(dtFromDate, 1);
		}
		return days;
	}

	public static List<CtCalcDate> listCalcWeeks(String fromDate, String toDate) {
		List<CtCalcDate> weeks = new ArrayList<>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		dtToDate = CtDateUtils.formatDayEndTime(dtToDate);
		while (dtToDate.compareTo(dtFromDate) >= 0) {

			Date theLastDayOfWeek = CtDateUtils.formatWeekLastDate(dtFromDate);
			if (theLastDayOfWeek.compareTo(dtToDate) > 0) {
				theLastDayOfWeek = dtToDate;
			}

			CtCalcDate calcDate = new CtCalcDate();
			//calcDate.setUnit(CtCalcConsts.UNIT_TYPE_WEEK);
			calcDate.setFromDate(new Date(dtFromDate.getTime()));
			calcDate.setToDate(new Date(theLastDayOfWeek.getTime()));

			weeks.add(calcDate);

			dtFromDate = CtDateUtils.addDate(theLastDayOfWeek, 1);
			dtFromDate = CtDateUtils.formatDayStartTime(dtFromDate);
		}
		return weeks;
	}

	public static List<CtCalcDate> listCalcMonths(String fromDate, String toDate) {
		List<CtCalcDate> months = new ArrayList<>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		dtToDate = CtDateUtils.formatDayEndTime(dtToDate);
		while (dtToDate.compareTo(dtFromDate) >= 0) {

			Date theLastDayOfMonth = CtDateUtils.formatMonthLastDate(dtFromDate);
			if (theLastDayOfMonth.compareTo(dtToDate) > 0) {
				theLastDayOfMonth = dtToDate;
			}

			CtCalcDate calcDate = new CtCalcDate();
			//calcDate.setUnit(CtCalcConsts.UNIT_TYPE_MONTH);
			calcDate.setFromDate(new Date(dtFromDate.getTime()));
			calcDate.setToDate(new Date(theLastDayOfMonth.getTime()));

			months.add(calcDate);

			dtFromDate = CtDateUtils.addDate(theLastDayOfMonth, 1);
			dtFromDate = CtDateUtils.formatDayStartTime(dtFromDate);
		}
		return months;
	}

	public static List<CtCalcDate> listLTVCalcDays(String fromDate, String toDate) {
		List<CtCalcDate> days = new ArrayList<CtCalcDate>();
		Date dtFromDate = CtDateUtils.stringToDate(fromDate, CtDateUtils.YYYY_MM_DD);
		Date dtToDate = CtDateUtils.stringToDate(toDate, CtDateUtils.YYYY_MM_DD);
		for (int ltvIndex = 0; ltvIndex < ltvNS.size(); ltvIndex++) {
			Date tempToDate = CtDateUtils.addDate(dtFromDate, ltvNS.get(ltvIndex));
			if (tempToDate.compareTo(dtToDate) > 0) {
				break;
			}
			CtCalcDate oneDate = new CtCalcDate();
			//oneDate.setUnit(CtCalcConsts.UNIT_TYPE_DAY);
			oneDate.setFromDate(new Date(dtFromDate.getTime()));
			oneDate.setToDate(new Date(tempToDate.getTime()));
			days.add(oneDate);
		}
		return days;
	}

}
